<?php

namespace mpend\modules\spread\models;

use Yii;
use mpend\models\User;
use common\models\user\UserBranch;
use common\models\member\Member;
use common\models\spread\SpreadAttract;

/**
 * 会员栏信息
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class SpreadReceiveForm extends \common\base\Model {

    /**
     * @var integer 会员用户ID
     */
    public $user_id;

    /**
     * @var string 用户手机号
     */
    public $mobile;

    /**
     * @var Member|null 会员模型
     */
    private $_member = null;

    /**
     * @var User|null 用户手机号
     */
    private $_user = null;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_id', 'mobile'], 'required'],
            [['user_id'], 'integer'],
            ['mobile', '\common\validators\MobileValidator'],
            ['user_id', 'validateMember'],
            ['mobile', 'validateUser'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'mobile' => '手机号',
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validateMember($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $this->_member = Member::findAvailable(['user_id' => $this->$attribute])->one();
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validateUser($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $user = User::findByMobile($this->$attribute);
        if ($user && $user->userBranch) {
            $this->addError($attribute, '抱歉，当前活动您不满足参加条件');
        }
        $this->_user = $user;
    }

    /**
     * 提交绑定
     */
    public function bind() {
        if (!$this->validate()) {
            $this->validateError('');
        }
        $member = $this->_member;
        if (!$member) {
            return null;
        }
        $user = $this->_user;
        if ($user) {
            $branch = new UserBranch();
            $branch->attributes = [
                'user_id' => $user->id,
                'member_id' => $member->id,
                'member_user_id' => $member->user_id,
                'status' => UserBranch::STATUS_ACTIVE,
            ];
            if (!$branch->save()) {
                return $branch->saveError();
            }
            return null;
        }


        $spread = SpreadAttract::find()->active()->andWhere([
            'mobile' => $this->mobile,
        ])->one();
        if (!$spread) {
            $spread = new SpreadAttract();
            $spread->attributes = [
                'user_id' => $this->user_id,
                'mobile' => $this->mobile,
            ];
            if (!$spread->save()) {
                return $spread->saveError();
            }
        }
        return $spread;
    }

}
